package com.xsprite.managesys.servlet;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.xsprite.managesys.bean.User;
import com.xsprite.managesys.service.UserService;

public class LoginServlet extends BaseServlet {
	private static final long serialVersionUID = 1L;
       
    public LoginServlet() {
        super();
    }
    
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		super.service(req, resp);
		if(isParameterNull("username") || isParameterNull("password")){
			return;
		}
		String username = queryParam("username");
		String password = queryParam("password");
//		rootPath = getServletConfig().getServletContext().getRealPath("/");
		User user = new User(username, password);
		UserService userService = new UserService();
		User resultUser = userService.queryUserByNameAndPassword(user);
		if(resultUser != null){
			req.getSession().setAttribute("userid", resultUser.getUserid());
			RequestDispatcher dispatcher = req.getRequestDispatcher("/view/functionChoose.jsp");
			dispatcher.forward(req, resp);
		} else {
			resp.sendRedirect("/ZZManageSys/index.jsp");
		}
	}
}
